package com.example.demo.dao;

import com.example.demo.bean.AdminBean;
import com.example.demo.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
    DBConnection DBConn = new DBConnection();
    private AdminBean adminBean;

    public boolean CheckLogin(String s1, String s2) throws SQLException {
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = null;
        boolean OK = true;
        String sql = "select * from admin where AdminName = '" + s1 + "' and AdminPwd = '" + s2 + "'";
        rs = stmt.executeQuery(sql);
        if (!rs.next()) {
            OK = false;
        }
        return OK;

    }

    public List<AdminBean> getList() {
        // todo 后期添加分页功能
        Connection conn = DBConn.getConn();
        String sql = "select * from admin";
        List<AdminBean> list = new ArrayList<AdminBean>();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                AdminBean adminBean = new AdminBean();
                adminBean.setAdminId(rs.getInt("AdminID"));
                adminBean.setAdminPwd(rs.getString("AdminPwd"));
                adminBean.setAdminType(rs.getInt("AdminType"));
                adminBean.setLastLoginTime(rs.getString("LastLoginTime"));
                adminBean.setAdminName(rs.getString("AdminName"));
                list.add(adminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException();
        }
        return list;
    }

    /**
     * 添加新闻
     *
     * @param
     * @return
     */
    public int add(AdminBean admin) {
        Connection conn = DBConn.getConn();
        String sql = "insert into admin value(null,?,?,?,?)";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            return pmst.executeUpdate(); // 执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 更新功能
     *
     * @param
     * @return
     */
    public int update(AdminBean admin) {
        Connection conn = DBConn.getConn();
        String sql = "update admin set AdminName=?, AdminPwd=?, AdminType=?, LastLoginTime=? where AdminID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            pmst.setInt(5, admin.getAdminId());
            System.out.println("执行sql：" + pmst.toString());
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int delete(int AdminId) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from admin where AdminID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, AdminId);
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
